package com.example.questionbase.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.questionbase.pojo.Question;
import com.example.questionbase.service.QuestionService;
import com.example.questionbase.util.Result;
import com.example.questionbase.util.WordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: fan
 * @Description: TODO
 * @Date: 2024/03/02 14:01
 */

@RestController
@RequestMapping("/question")
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    /**
     * 保存问题
     *
     * @param text
     * @return
     */
    @GetMapping("/save")
    public Result saveQuestion(@RequestParam(name = "text") String text) {
        List<String> keywords = WordUtil.extractKeywords(text);
        QueryWrapper<Question> wrapper = new QueryWrapper<>();
        wrapper.eq("keyword", WordUtil.listToString(keywords));
        Question question = questionService.getOne(wrapper);
        if (question == null) {
            Question newQuestion = new Question(text, keywords, 1);
            questionService.save(newQuestion);
        } else {
            int frequency = question.getFrequency() + 1;
            UpdateWrapper<Question> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id", question.getId()).set("frequency", frequency);
            questionService.update(updateWrapper);
        }
        return Result.buildSuccessResult();
    }

    /**
     * 检测关键词是否需要入库
     *
     * @param text
     * @param keyword
     * @return
     */
    @GetMapping("/check")
    public Result checkKeyword(@RequestParam(name = "text") String text,
                               @RequestParam(name = "keyword") String keyword) {
        List<String> keywords = WordUtil.extractKeywords(text);
        if (keywords.contains(keyword)) {
            return Result.buildSuccessResultWithData("该关键词可以识别，无需加入词库");
        }
        return Result.buildSuccessResultWithData("该关键词无法识别，需要加入词库");
    }

    /**
     * 推荐问题
     *
     * @param text
     * @return
     */
    @PostMapping("/recommend")
    public Result recommend(@RequestParam(name = "text", defaultValue = "") String text,
                            @RequestParam(name = "count", defaultValue = "20") String count) {
        QueryWrapper<Question> wrapper = new QueryWrapper<>();
        if (text.isEmpty()) {
            // 未输入任何关键词
            // 按数据库热搜推荐
            wrapper.orderByDesc("frequency");
        } else {
            // TODO 过于粗暴，是否采取分词之后分别对分词进行查询
            wrapper.like("keyword", text)
                    .orderByDesc("frequency");
        }
        wrapper.orderByDesc("last_viewed_time")
                .last("LIMIT " + count);
        List<Question> questions = questionService.list(wrapper);
        return Result.buildSuccessResultWithData(questions);
    }
}
